﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Aliyeye.Car.Net.Utility;

namespace Aliyeye.Car.Net.Dal
{
    public class Accounts_Permissions
    {
        public Accounts_Permissions()
        { }
        public int Add(Model.Accounts_Permissions model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@PermissionID", SqlDbType.Int,4),
					new SqlParameter("@Description", SqlDbType.VarChar,255),
					new SqlParameter("@CategoryID", SqlDbType.Int,4)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.Description;
            parameters[2].Value = model.CategoryID;

            int roleId = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Accounts_Permissions_ADD", parameters);
            if (roleId > 0)
            {
                return roleId;
            }
            else
            {
                return 0;
            }
        }
        public bool Update(Model.Accounts_Permissions model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@PermissionID", SqlDbType.Int,4),
					new SqlParameter("@Description", SqlDbType.VarChar,255),
					new SqlParameter("@CategoryID", SqlDbType.Int,4)};
            parameters[0].Value = model.PermissionID;
            parameters[1].Value = model.Description;
            parameters[2].Value = model.CategoryID;

            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Accounts_Permissions_Update", parameters);

            if (intResult > 0)
                return true;
            else
                return false;
        }
        public bool Delete(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@PermissionID", SqlDbType.Int,4)};
            parameters[0].Value = Id;
            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Accounts_Permissions_Delete", parameters);

            if (intResult > 0)
                return true;
            else
                return false;
        }
        public bool DeleteCoun(int CategoryID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Accounts_Permissions ");
            strSql.Append(" where CategoryID=@CategoryID ");
            SqlParameter[] parameters = {
					new SqlParameter("@CategoryID", SqlDbType.Int,4)};
            parameters[0].Value =CategoryID;
            int intResult = SqlHelper.ExecuteNonQuery(CommandType.Text,strSql.ToString(), parameters);

            if (intResult > 0)
                return true;
            else
                return false;
        }

        public Model.Accounts_Permissions GetModel(int ClassID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@PermissionID", SqlDbType.Int,4)};
            parameters[0].Value = ClassID;

            Model.Accounts_Permissions model = new Model.Accounts_Permissions();
            DataSet ds = SqlHelper.GetDataFormDataSet(CommandType.StoredProcedure, "Accounts_Permissions_GetModel", parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["PermissionID"].ToString() != "")
                {
                    model.PermissionID = int.Parse(ds.Tables[0].Rows[0]["PermissionID"].ToString());
                }
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                if (ds.Tables[0].Rows[0]["CategoryID"].ToString() != "")
                {
                    model.CategoryID = int.Parse(ds.Tables[0].Rows[0]["CategoryID"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PermissionID,Description,CategoryID ");
            strSql.Append(" FROM Accounts_Permissions ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.Query(strSql.ToString());
        }
    }
}
